using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support.Javascript;
using Sedna.Core.DataBinding;
using Sedna.Core.Tiles.Contents;
using DataBinder = Sedna.Core.DataBinding.DataBinder;

namespace Sedna.Core.Controls.Support
{
	/// <summary>
	/// Base class for labels.
	/// </summary>
	public abstract class BaseLabel: System.Web.UI.WebControls.Label, IContent, IConfigurationAware, IControlDataLoadAware, IClientScriptingControl
	{
		private string clientVariable;
		private bool clientScriptingEnabled;

		protected ILabelConfiguration configuration;
		protected string configurationId;
		private bool nowrap=true;
        private string javascriptClass;

		private String contentPlaceHolderId;

		protected IDataBinder binder;

        #region Properties

        public String ContentPlaceHolderId
		{
			get { return contentPlaceHolderId;}
			set { contentPlaceHolderId = value;}
		}

        #region Javascript support
	    
        public virtual bool ClientScriptingEnabled
        {
            get { return clientScriptingEnabled&&Visible; }
            set { clientScriptingEnabled = value; }
        }
	    
        public virtual string ClientVariable
        {
            get { return /*clientVariable*/ ControlUtil.GetClientVariable(this, clientVariable); }
            set { clientVariable = value; }
        }

        public string JavascriptClass
        {
            get { return javascriptClass; }
            set { javascriptClass = value; }
        }

        public string NodeName
        {
            get { return ClientID; }
        }


        #endregion

	    public ILabelConfiguration Configuration
	    {
	        get { return configuration; }
	        set { configuration = value; }
	    }

	    IConfiguration IConfigurationAware.Configuration
		{
			get { return configuration; }
			set { this.configuration = (ILabelConfiguration)value; }
		}

		public string ConfigurationId
		{
			get { return configurationId; }
			set { this.configurationId = value; }
		}

		public bool Nowrap
		{
			get { return nowrap; }
			set { nowrap = value; }
        }

        #endregion

        public BaseLabel():base()
		{
		}

		public BaseLabel(IConfiguration configuration) : this()
		{
			this.configuration = (ILabelConfiguration)configuration;
			this.configurationId = configuration.ObjectName;
		    
            BuildFromControl();
		}

		protected void BuildFromControl()
		{
			this.ID = configuration.Id;
		    
			if (!Configuration.Width.IsEmpty)
			{
				Width=Configuration.Width;
			}

			if (!Configuration.Height.IsEmpty)
			{
				Height=Configuration.Height;
			}

            Nowrap = configuration.Nowrap;
		    
			ClientScriptingEnabled = configuration.ClientScriptingEnabled;
            ClientVariable = configuration.ClientVariable;

            Visible = configuration.Visible && configuration.IsVisibleForPrincipal();
            Enabled = configuration.Enabled && configuration.IsEnabledForPrincipal();
		    
            JavascriptClass = configuration.JavascriptClass;

            if (configuration.CssClass != null)
            {
                CssClass = configuration.CssClass;
            }
		    
            ControlUtil.SetupClientAttributes(this, configuration);
        }

        protected virtual void Build()
        {
        }

		protected override void OnInit(EventArgs e)
		{
			base.OnInit (e);
			if (configuration == null)
			{
				configuration = (ILabelConfiguration)SpringContextFactory.GetContext().GetObject(configurationId);
                BuildFromControl();
			}
			binder=new DataBinder(this, configuration, ExecutionContext.GetCurrent(ClientID));
            Build();
		}


		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
			Unbind();
			ControlUtil.ExecuteOnLoad(executionContext, Context, Configuration, this.ClientID);

			// Assigning visibility to analyze this property
			// later in OnPreRender method from ExecutionContext
            if (Configuration.VisibleBindPath != null)
			    this.Visible = true;
		}
		
		

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
			ControlUtil.ExecuteOnPreRender(executionContext,Context, Configuration, this.ClientID);
			Bind();
			if (ClientScriptingEnabled)
			{
//				EnableClientScripting();
                DeclareScriptVariable();

			}
        }

        #region Javascript support

        private void RenderScriptAttributes(HtmlTextWriter writer)
        {
            foreach (DictionaryEntry entry in BuildScriptParameters())
            {
                writer.AddAttribute("sedna:" + (string)entry.Key, (string)entry.Value);
            }
        }

        protected virtual IDictionary BuildScriptParameters()
        {
            IDictionary parameters = new ListDictionary();
            parameters.Add("jsClass", JavascriptClass);
            return parameters;
        }
	    
        private void DeclareScriptVariable()
        {
            ControlUtil.EnsureIdAttribute(this);
            JavascriptController.Current.DeclareClass(JavascriptClass);
            if (ClientVariable != null)
            {
                JavascriptController.Current.DeclareObjectByNode(ClientVariable, JavascriptClass, NodeName);
            }
        }

        #endregion


//		protected void EnableClientScripting()
//		{
//			ControlUtil.EnsureIdAttribute(this);
//
//            JavascriptController.Current.DeclareClass("Label");
//            JavascriptController.Current.DeclareObjectWithQuotedParams(ClientVariable, "Label", ClientID);
//		}

		public void OnDataLoad()
		{
			IDictionary executionContext = ExecutionContext.GetCurrent(ClientID);
			ControlUtil.ExecuteOnDataLoad(executionContext, Context, Configuration, this.ClientID);
		}

	    protected override void AddAttributesToRender(HtmlTextWriter writer)
	    {
	        base.AddAttributesToRender(writer);
            if (ClientScriptingEnabled)
            {
                RenderScriptAttributes(writer);
            }
        }

	    protected override void RenderContents(HtmlTextWriter writer)
		{
			if (!nowrap)
			{
				base.RenderContents(writer);
				return;
			}
			writer.RenderBeginTag(HtmlTextWriterTag.Nobr);
			base.RenderContents(writer);
			writer.RenderEndTag();
		}

		protected virtual void Bind()
		{
			binder.Bind("Text");	
		}

		protected virtual void Unbind()
		{
			binder.Unbind("Text");	
		}


	}
}
